var app = getApp(), api = getApp().api, is_no_more = !1, is_loading = !1, p = 2;

Page({
    data: {
        naver:'stat',
        status: -1,
        order_list: [],
        show_no_data_tip: !1,
        hide: 1,
        qrcode: "",
        return_integral:0
    },
    onLoad: function (t) {
        getApp().page.onLoad(this, t);
        var e = this;
        is_loading = is_no_more = !1, p = 2, e.setData({
            options: t
        }), e.loadOrderList(t.status || -1), getCurrentPages().length < 2 && e.setData({
            show_index: !0
        });
    },

    loadOrderList1: function (t) {
        var o = this;
        var order_list = o.data.order_list
        t != 4 && (
            null == t && (t = -1), getApp().core.showLoading({
                title: "正在加载",
                mask: !0
            }), getApp().request({
                url: getApp().api.integral.list,
                data: {
                    status: t
                },
                success: function (e) {
                    if (e.code == 0) {
                        console.log('22')
                        for (var i = 0; i < e.data.list.length; i++) {
                            order_list.push(e.data.list[i])
                        }
                        order_list.sort(function (a, b) {
                            return b.addtime < a.addtime ? -1 : 1
                        })
                        o.setData({
                            order_list: order_list,
                            status: t
                        });
                        console.log('123', o.data.order_list)
                    }
                    // 0 == e.code && 
                },
                complete: function () {
                    getApp().core.hideLoading();
                }
            }));
    },
    loadOrderList: function (t1) {
        null == t1 && (t1 = -1);
        var e = this;
        var order_list = e.data.order_list
        e.setData({
            status: t1
        }), getApp().core.showLoading({
            title: "正在加载",
            mask: !0
        });
        var a = {
            status: e.data.status
        };
        e.data.options;
        void 0 !== e.data.options.order_id && (a.order_id = e.data.options.order_id), getApp().request({
            url: getApp().api.order.list,
            data: a,
            success: function (t) {
                if (t.code == 0) {
                    console.log('11')
                    for (var i = 0; i < t.data.list.length; i++) {
                        order_list.push(t.data.list[i])
                    };
                    e.setData({
                        order_list: order_list,
                        return_integral:t.data.return_integral,
                        pay_type_list: t.data.pay_type_list
                    }), e.loadOrderList1(t1), getApp().core.getStorageSync(getApp().const.ITEM) && getApp().core.removeStorageSync(getApp().const.ITEM)
                }
                e.setData({
                    show_no_data_tip: 0 == e.data.order_list.length
                });
            },
            complete: function () {
                getApp().core.hideLoading();
            }
        });
    },
    onReachBottom: function () {
        var a = this;
        is_loading || is_no_more || (is_loading = !0, getApp().request({
            url: getApp().api.order.list,
            data: {
                status: a.data.status,
                page: p
            },
            success: function (t) {
                if (0 == t.code) {
                    var e = a.data.order_list.concat(t.data.list);
                    a.setData({
                        order_list: e,
                        pay_type_list: t.data.pay_type_list
                    }), 0 == t.data.list.length && (is_no_more = !0);
                }
                p++;
            },
            complete: function () {
                is_loading = !1;
            }
        }));
    },
    orderPay_1: function (e) {
        var a = this, t = a.data.pay_type_list;
        1 == t.length ? (getApp().core.showLoading({
            title: "正在提交",
            mask: !0
        }), 0 == t[0].payment && a.WechatPay(e), 3 == t[0].payment && a.BalancePay(e)) : getApp().core.showModal({
            title: "提示",
            content: "选择支付方式",
            cancelText: "余额支付",
            confirmText: "线上支付",
            success: function (t) {
                getApp().core.showLoading({
                    title: "正在提交",
                    mask: !0
                }), t.confirm ? a.WechatPay(e) : t.cancel && a.BalancePay(e);
            }
        });
    },
    WechatPay: function (t) {
        console.log('本期推荐付款')
        getApp().request({
            url: getApp().api.order.pay_data,
            data: {
                order_id: t.currentTarget.dataset.id,
                pay_type: "WECHAT_PAY"
            },
            complete: function () {
                getApp().core.hideLoading();
            },
            success: function (t) {
                0 == t.code && getApp().core.requestPayment({
                    _res: t,
                    timeStamp: t.data.timeStamp,
                    nonceStr: t.data.nonceStr,
                    package: t.data.package,
                    signType: t.data.signType,
                    paySign: t.data.paySign,
                    success: function (t) {
                    },
                    fail: function (t) { },
                    complete: function (t) {
                        "requestPayment:fail" != t.errMsg && "requestPayment:fail cancel" != t.errMsg ? getApp().core.requestSubscribeMessage({
                            tmplIds: ['_XGsVLLisG8_LTbDnBk_4yIr2GbwvkNi8aSsKiUN_80'],
                            success(res) {
                                console.log('订阅消息', res)
                                if(res == "requestSubscribeMessage:ok"){
                                    getApp().core.redirectTo({
                                        url: "/pages/order/order?status=1"
                                    }) 
                                }else{
                                    getApp().core.redirectTo({
                                        url: "/pages/order/order?status=1"
                                    }) 
                                }
                            }
                        }): getApp().core.showModal({
                            title: "提示",
                            content: "订单尚未支付",
                            showCancel: !1,
                            confirmText: "确认",
                            success: function (t) {
                                t.confirm && getApp().core.redirectTo({
                                    url: "/pages/order/order?status=0"
                                });
                            }
                        });
                    }
                }), 1 == t.code && getApp().core.showToast({
                    title: t.msg,
                    image: "/images/icon-warning.png"
                });
            }
        });
    },
    orderSubmitPay: function(e) {
        var t = e.currentTarget.dataset;
        console.log('开心乐园付款')
        getApp().core.showLoading({
            title: "提交中",
            mask: !0
        }), getApp().request({
            url: getApp().api.integral.order_submit,
            data: {
                id: t.id
            },
            success: function(e) {
                0 == e.code ? (getApp().core.hideLoading(), getApp().core.requestPayment({
                    _res: e,
                    timeStamp: e.data.timeStamp,
                    nonceStr: e.data.nonceStr,
                    package: e.data.package,
                    signType: e.data.signType,
                    paySign: e.data.paySign,
                    success: function(e) {
                    },
                    complete: function(e) {
                        "requestPayment:fail" != e.errMsg && "requestPayment:fail cancel" != e.errMsg ? "requestPayment:ok" == e.errMsg &&getApp().core.requestSubscribeMessage({
                            tmplIds: ['_XGsVLLisG8_LTbDnBk_4yIr2GbwvkNi8aSsKiUN_80'],
                            success(res) {
                                console.log('订阅消息', res)
                                if(res == "requestSubscribeMessage:ok"){
                                    getApp().core.redirectTo({
                                        url: "/pages/integral-mall/order/order?status=1"
                                    }) 
                                }else{
                                    getApp().core.redirectTo({
                                        url: "/pages/integral-mall/order/order?status=1"
                                    }) 
                                }
                            }
                        }): getApp().core.showModal({
                            title: "提示",
                            content: "订单尚未支付",
                            showCancel: !1,
                            confirmText: "确认"
                        });
                    }
                })) : (getApp().core.hideLoading(), getApp().core.showModal({
                    title: "提示",
                    content: e.msg,
                    showCancel: !1,
                    confirmText: "确认"
                }));
            }
        });
    },
    BalancePay: function (t) {
        getApp().request({
            url: getApp().api.order.pay_data,
            data: {
                order_id: t.currentTarget.dataset.id,
                pay_type: "BALANCE_PAY"
            },
            complete: function () {
                getApp().core.hideLoading();
            },
            success: function (t) {
                0 == t.code && getApp().core.redirectTo({
                    url: "/pages/order/order?status=1"
                }), 1 == t.code && getApp().core.showModal({
                    title: "提示",
                    content: t.msg,
                    showCancel: !1
                });
            }
        });
    },
    orderRevoke: function (e) {
        var a = this;
        console.log(e)
        var miniapp_order_type = e.currentTarget.dataset.miniapp_order_type
        if (miniapp_order_type == 0) {
            //推荐商品
            var urlorder = getApp().api.order.revoke
        } else {
            //开心乐园商品
            var urlorder = getApp().api.integral.revoke
        }
        getApp().core.showModal({
            title: "提示",
            content: "是否取消该订单？",
            cancelText: "否",
            confirmText: "是",
            success: function (t) {
                if (t.cancel) return !0;
                t.confirm && (getApp().core.showLoading({
                    title: "操作中"
                }), getApp().request({
                    url: urlorder,
                    data: {
                        order_id: e.currentTarget.dataset.id
                    },
                    success: function (t) {
                        getApp().core.hideLoading(), getApp().core.showModal({
                            title: "提示",
                            content: t.msg,
                            showCancel: !1,
                            success: function (t) {
                                t.confirm &&(a.setData({order_list: []}),a.loadOrderList(a.data.status));
                            }
                        });
                    }
                }));
            }
        });
    },
    orderConfirm: function (e) {
        var a = this;
        console.log(e)
        var miniapp_order_type = e.currentTarget.dataset.miniapp_order_type
        if (miniapp_order_type == 0) {
            //推荐商品
            var urlorder = getApp().api.order.confirm
        } else {
            //开心乐园商品
            var urlorder = getApp().api.integral.confirm
        }
        getApp().core.showModal({
            title: "提示",
            content: "是否确认已收到货？",
            cancelText: "否",
            confirmText: "是",
            success: function (t) {
                if (t.cancel) return !0;
                t.confirm && (getApp().core.showLoading({
                    title: "操作中"
                }), getApp().request({
                    url: urlorder,
                    data: {
                        order_id: e.currentTarget.dataset.id
                    },
                    success: function (t) {
                        getApp().core.hideLoading(), getApp().core.showToast({
                            title: t.msg
                        }), 0 == t.code && (a.setData({order_list: []}),a.loadOrderList(3));
                    }
                }));
            }
        });
    },
    orderQrcode: function (t) {
        var e = this, a = e.data.order_list, o = t.target.dataset.index;
        getApp().core.showLoading({
            title: "正在加载",
            mask: !0
        }), e.data.order_list[o].offline_qrcode ? (e.setData({
            hide: 0,
            qrcode: e.data.order_list[o].offline_qrcode
        }), getApp().core.hideLoading()) : getApp().request({
            url: getApp().api.order.get_qrcode,
            data: {
                order_no: a[o].order_no
            },
            success: function (t) {
                0 == t.code ? e.setData({
                    hide: 0,
                    qrcode: t.data.url
                }) : getApp().core.showModal({
                    title: "提示",
                    content: t.msg
                });
            },
            complete: function () {
                getApp().core.hideLoading();
            }
        });
    },
    hide: function (t) {
        this.setData({
            hide: 1
        });
    },
    onShow: function () {
        getApp().page.onShow(this);
    },
    express_detail:function(r){
        console.log(r)
        var id = r.currentTarget.dataset.id
        var type = r.currentTarget.dataset.type
        if (type == 2) {
            var data = {
                order_id: id,
                type: "IN" //开心乐园
                // type: "SP" //超惊喜
            }
        } else if (type == 3) {
            var data = {
                order_id: inId,
                // type: "IN" //开心乐园
                type: "SP" //超惊喜
            }
        } else {
            var data = {
                order_id: id,
                type: "mall"
            }
        }
        getApp().request({
            url: getApp().api.order.express_list,
            data: data,
            success: function (t) {
                console.log(t)
                if(t.count>1){
                    var irlist = []
                    for (var s in t.list){
                        irlist.push(t.list[s].express)
                    }
                    wx.showActionSheet({
                        itemList: irlist,
                        success (res) {
                            wx.navigateTo({
                                url: "/pages/express-detail/express-detail?id=" + id + "&type=" + type,
                            })
                        },
                        fail (res) {
                          console.log(res.errMsg)
                        }
                      })                      
                }else{
                    wx.navigateTo({
                      url: "/pages/express-detail/express-detail?id=" + id + "&type=" + type,
                    })
                }
            }
        });
    },
});